/*
 * @Author: jiu yin
 * @Date: 2023-05-15 14:50:04
 * @LastEditTime: 2023-06-01 16:21:57
 * @LastEditors: jiu yin zhen jing
 * @FilePath: \init_react\src\redux\modules\keepActive.ts
 * jiu
 */
import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import type { PayloadAction } from "@reduxjs/toolkit";
import type { routeArrItem } from "@/type/index.ts";

// 异步
export const asyncFn = createAsyncThunk("test", async (value: any) => {
   return Promise.resolve(value);
});

export const keepActive = createSlice({
   name: "keepActive",
   initialState: { arr: [] as any[]},
   reducers: {
      push: (state, action: PayloadAction<routeArrItem>) => {
         state.arr.push(action.payload);
      },
      change:(state, action: PayloadAction<any[]>) => {
         state.arr=action.payload;
      },
      remove:(state, action: PayloadAction<number>) => {
         state.arr.splice(action.payload,1)
      }
   },
   //异步
   extraReducers: builder => {
      builder.addCase(asyncFn.fulfilled, (state, action) => {
         console.log(state.arr,"撒大声地sd ada d");
         state.arr = action.payload; // 返回值
      });
   },
});

// Action creators are generated for each case reducer function
export const { push ,change,remove} = keepActive.actions;

export default keepActive.reducer;
